package com.qyy.www_chatroom.mapper;

import com.qyy.www_chatroom.model.Message;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface MessageMapper {

    //获取指定对话的最后一条消息
    @Select("select content from message where sessionId = #{sessionId} order by  postTime desc limit 1")
    String getLastMessageBySessionId(int sessionId);


    //获取历史消息
    //有的会话，历史消息特别多。
    //此处做一个限制，默认只取最近100条消息
    //进行笛卡尔积。username = fromId。根据sessionId匹配
    @Select("select messageId,fromId,username as fromName,sessionId,content" +
            " from user,message " +
            "where userId = fromId and sessionId = #{sessionId} " +
            "order by postTime desc limit 100")
    List<Message> getMessageBySessionId(int sessionId);


    //通过这个方法实现插入消息到数据库
    @Insert("insert into message values (null,#{fromId},#{sessionId},#{content},now())")
    void add(Message message);
}
